﻿using BP.DA;
using BP.En;
using BP.Sys;
using BP.WF.Template.Frm;
using System.Runtime.CompilerServices;

namespace BP.Sys.Printer
{

    /// <summary>
    /// 数据源属性
    /// </summary>
    public class PTDBSrcAttr : EntityMyPKAttr
    {
        /// <summary>
        /// 表单ID
        /// </summary>
        public const string FrmID = "FrmID";
        /// <summary>
        /// 表单名称
        /// </summary>
        public const string FrmName = "FrmName";
        /// <summary>
        /// 模板ID
        /// </summary>
        public const string FrmPrintTemplateID = "FrmPrintTemplateID";
        /// <summary>
        /// 数据源类型ID
        /// </summary>
        public const string DBTypeID = "DBTypeID";
        /// <summary>
        /// 类型名称
        /// </summary>
        public const string DBTypeName = "DBTypeName";
        /// <summary>
        /// 单据/实体ID
        /// </summary>
        public const string RefFrmID = "RefFrmID";
        /// <summary>
        /// 单据/实体名称
        /// </summary>
        public const string RefFrmName = "RefFrmName";
        /// <summary>
        /// 数据源
        /// </summary>
        public const string DBSrc = "DBSrc";
        /// <summary>
        /// 查询表达式
        /// </summary>
        public const string SQLSelect = "SQLSelect";
    }
    /// <summary>
    /// 数据源
    /// </summary>
    public class PTDBSrc : EntityMyPK
    {
        #region  属性
        /// <summary>
        /// UI界面上的访问控制
        /// </summary>
        public override UAC HisUAC
        {
            get
            {
                UAC uac = new UAC();
                uac.OpenForSysAdmin();
                return uac;
            }
        }
        /// <summary>
        /// 编号
        /// </summary>
        public new string MyPK
        {
            get
            {
                string no = GetValStrByKey("MyPK");
                no = no.Replace("\n", "");
                no = no.Replace(" ", "");
                return no;
            }
            set
            {
                SetValByKey("MyPK", value);
            }
        }
        public int FrmRefPKModel
        {
            get
            {
                return GetValIntByKey("FrmRefPKModel");
            }
        }

        public string FrmRefPKAttrKey
        {
            get
            {
                string val= GetValStringByKey("FrmRefPKAttrKey");
                if (DataType.IsNullOrEmpty(val) == true)
                    return "OID"; 
                return val;
            }
        }
        public string FrmRefPKAttrName
        {
            get
            {
                return GetValStringByKey("FrmRefPKAttrName");
            }
        }

        public string FrmID
        {
            get
            {
                return GetValStringByKey(PTDBSrcAttr.FrmID);
            }
            set
            {
                SetValByKey(PTDBSrcAttr.FrmID, value);
            }
        }
      
        
        public string RefFrmID
        {
            get
            {
                return GetValStringByKey(PTDBSrcAttr.RefFrmID);
            }
            set
            {
                SetValByKey(PTDBSrcAttr.RefFrmID, value);
            }
        }
        public string RefFrmName
        {
            get
            {
                return GetValStringByKey(PTDBSrcAttr.RefFrmName);
            }
            set
            {
                SetValByKey(PTDBSrcAttr.RefFrmName, value);
            }
        }
        public string DBTypeID
        {
            get
            {
                return GetValStringByKey(PTDBSrcAttr.DBTypeID);
            }
            set
            {
                SetValByKey(PTDBSrcAttr.DBTypeID, value);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 数据源
		/// </summary>
		public PTDBSrc() { }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="mypk">主键</param>
        public PTDBSrc(string mypk) : base(mypk.Replace("\n", "").Trim())
        {
        }
        /// <summary>
        /// 重写基类方法
        /// </summary>
        public override Map EnMap
        {
            get
            {
                if (_enMap != null)
                    return _enMap;
                Map map = new Map("Sys_FrmPrintTemplateDBSrc", "数据源数据源");
                map.IndexField = PTDBSrcAttr.FrmID;

                map.AddMyPK();

                map.AddTBString("FrmID", null,"表单ID", false, false, 0, 60, 60);
                map.AddTBString("FrmName", null,"表单名称", false, false, 0, 60, 60);
                map.AddTBString("FrmPrintTemplateID", null,"模板ID", false, false, 0, 60, 60);

                //Bill=单据,EntityNoName=实体,SQL=数据源,Img=图片,Ath=附件
                map.AddTBString("DBTypeID", null,"数据源类型ID", true, true, 0, 60, 60);
                map.AddTBString("DBTypeName", null,"类型名称", true, true, 0, 60, 60);


                map.AddTBInt("FrmRefPKModel", 0, "关联主键", false, true);
                map.AddTBString("FrmRefPKAttrKey", null, "主键(宿主表的字段)", true, true, 0, 60, 60);
                map.AddTBString("FrmRefPKAttrName", null, "主键名称", true, true, 0, 60, 60);



                //单据/实体ID
                map.AddTBString("RefFrmID", null,"单据/实体ID", false, true, 0, 60, 60);
                map.AddTBString("RefFrmName", null,"单据/实体名称", false, true, 0, 60, 60);

                //SQL.
                map.AddTBString("DBSrc", null,"数据源", false, true, 0, 500, 60);
                map.AddTBString("SQLSelect", null,"查询表达式", false, true, 0, 500, 60);

                map.AddTBInt("Idx", 0,"Idx", false, false);
                map.AddTBAtParas(4000);

                _enMap = map;
                return _enMap;
            }
        }
        #endregion
        protected override bool beforeInsert()
        {
            return base.beforeInsert();
        }
    }
    /// <summary>
    /// 数据源s
    /// </summary>
    public class PTDBSrcs : EntitiesMyPK
    {
        #region 构造
        /// <summary>
        /// 得到它的 Entity 
        /// </summary>
        public override Entity GetNewEntity
        {
            get
            {
                return new PTDBSrc();
            }
        }
        /// <summary>
        /// 数据源
        /// </summary>
        public PTDBSrcs()
        {
        }
        public PTDBSrcs(string templateID)
        {
            this.Retrieve(PTDBSrcAttr.FrmPrintTemplateID, templateID);

            PrintTemplate template = new PrintTemplate(templateID);
            PTDBSrc src= new PTDBSrc();
            src.FrmID = template.FrmID;
            src.RefFrmID = template.FrmID;
            this.AddEntity(src);
        }
        #endregion

        #region 为了适应自动翻译成java的需要,把实体转换成List.
        /// <summary>
        /// 转化成 java list,C#不能调用.
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.IList<PTDBSrc> ToJavaList()
        {
            return (System.Collections.Generic.IList<PTDBSrc>)this;
        }
        /// <summary>
        /// 转化成list
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.List<PTDBSrc> Tolist()
        {
            System.Collections.Generic.List<PTDBSrc> list = new System.Collections.Generic.List<PTDBSrc>();
            for (int i = 0; i < Count; i++)
            {
                list.Add((PTDBSrc)this[i]);
            }
            return list;
        }
        #endregion 为了适应自动翻译成java的需要,把实体转换成List.
    }

}
